﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace WooCoo.DataPortal.Server
{
    /// <summary>
    /// 服务端的数据门户访问入口，消息路由类
    /// </summary>
    public class DataPortalFacade : IDataPortalServer
    {
        #region Constructors
        /// <summary>
        /// Default constructor
        /// </summary>
        public DataPortalFacade()
        { }

        #endregion

        #region Data Access

        /// <summary>
        /// 通过服务端创建新的业务对象.
        /// </summary>
        /// <param name="objectType">业务对象类型.</param>
        /// <param name="criteria">创建条件.</param>
        /// <param name="context">
        /// <see cref="Server.DataPortalContext" /> 传递到服务端的上下文.
        /// </param>
        public DataPortalResult Create(object entityOrList, DataPortalContext context)
        {
            try
            {
                //SetContext(context);
                var portal = new SimplePortal();
                var result = portal.Create(entityOrList, context);

                return result;
            }
            finally
            {
                //ClearContext(context);
            }
        }

        /// <summary>
        /// 获取业务对象.
        /// </summary>
        /// <param name="objectType">业务对象类型.</param>
        /// <param name="criteria">条件.</param>
        /// <param name="context">
        /// <see cref="Server.DataPortalContext" /> 传递到服务端的上下文.
        /// </param>
        public DataPortalResult Fetch(Type objectType, object criteria, DataPortalContext context)
        {
            try
            {
                //SetContext(context);

                var portal = new SimplePortal();
                var result = portal.Fetch(objectType, criteria, context);
                return result;
            }
            finally
            {
                //ClearContext(context);
            }
        }

        /// <summary>
        /// 更新业务对象.
        /// </summary>
        /// <param name="obj">需要更新的对象实例.</param>
        /// <param name="context">
        /// <see cref="Server.DataPortalContext" /> 传递到服务端的业务对象.
        /// </param>
        public DataPortalResult Update(object obj, DataPortalContext context)
        {
            try
            {
                //SetContext(context);
                var portal = new SimplePortal();
                var result = portal.Update(obj, context);
                return result;
            }
            finally
            {
                //ClearContext(context);
            }
        }

        /// <summary>
        /// 删除一个业务对象.
        /// </summary>
        /// <param name="objectType">需要删除的业务对象类型.</param>
        /// <param name="criteria">条件.</param>
        /// <param name="context">
        /// <see cref="Server.DataPortalContext" /> 传递到服务端的上下文.
        /// </param>
        public DataPortalResult Delete(Type objectType, object criteria, DataPortalContext context)
        {
            try
            {
                //SetContext(context);

                var portal = new SimplePortal();
                var result = portal.Delete(objectType, criteria, context);
                return result;
            }
            finally
            {
                //ClearContext(context);
            }
        }

        /// <summary>
        /// 执行一个操作
        /// </summary>
        /// <param name="requestMethod">操作方法对象</param>
        /// <param name="context">传递到服务端的上下文</param>
        /// <returns></returns>
        public DataPortalResult Execute(object target,ActionInfo requestMethod, DataPortalContext context)
        {
            try
            {
                //SetContext(context);

                var portal = new SimplePortal();
                var result = portal.Execute(target,requestMethod, context);
                return result;
            }
            finally
            {
                //ClearContext(context);
            }
        }
        #endregion
    }
}
